import {putObj,delObj,fetchList} from '../../../api/finance-manage-receipt'
import {mapState} from "vuex";
import showData from '../edit/show-data'
import printData from '../edit/print-data'

export default {
    components:{showData, printData},
		provide () {
			return {
				initList: this.getListData
			};
		},
    mounted() {
				this.getListData();
    },
    data(){
        return {
            tableData: {},
            selectionList:[],
            disabled:false,
						// 请求参数
						params: {
							current: 1,
							size: 20,
							templateName: ""
						},
						// 分页
						pager: {
							total: 0, // 总页数
							current: 1, // 当前页数
							size: 20 // 每页显示多少条
						},
						// 页面loading
						loading: false,
						filterList: [
							{
								label: "适用项目",
								inputType: "dict",
								dictName: "project",
								name: "project",
								multiple: true
							}
						]
        }
    },
    computed: mapState({
        project: state => {
            let data = state.dict.find(item => item.type === "project") || {}
            // // let list = data.children || []
            return data.children || []
        },
    }),
    methods:{
				getListData (params = {}) {
					this.loading = true;
					let requestData = { ...this.params, ...params };
					fetchList(requestData)
						.then(res => {
							const { current, size, total, records } = res.data.data;
							this.tableData = records;
							this.params = requestData;
							this.pager = { total, current, size };
							this.disabled=false
						})
						.finally(() => {
							this.loading = false;
						});
				},
        print(){
            if (this.selectionList.length > 1) {
                this.err('只能选择一个收据模板设置')
            }else {
                this.$refs.printData.show(this.selectionList)
            }
        },
        children:function () {
            let children=[]
            for(let i=0;i<this.tableData.length;i++){
                let ch={}
                ch.label=this.tableData[i].project
                ch.value=this.tableData[i].project
                ch.type='input',
                children.push(ch)
            }
            const res = new Map();
            children=children.filter(children=>!res.has(children.value)&&res.set(children.value,1))
            return children
        },
        delData(){
            if (this.selectionList.length === 0) {
                this.err('请选择要删除的收据模板设置')
            } else {
                this.confirm('删除确认', '您确定要删除该数据吗？', () => {
                    let ids = this.selectionList.map(item => item.receiptId)
                    delObj(ids.join(',')).then(resp => {
                        if (resp.data.data) {
                            this.suc('删除成功')
                            this.getListData()
                        } else {
                            this.err('删除失败')
                        }
                    })
                })
            }
        },
        showData(item){
            this.$refs.showData.show(item)
        },
        editData(item){
            this.$router.push({
                path: `/finance-manage-receipt`,
                query:item
            })
        },
        change(val){
            this.disabled=true
            putObj(val).then(res =>{
                this.getListData()
            })
        },
			// 菜单点击监听
			handleMenuClick (type) {
				switch (type) {
					case "add":
						this.editData();
						break;
					case "delete":
						this.delData();
						break;
					case "export":
						this.print();
						break;
					default:
						this.$message(type);
						break;
				}
			}
    }
}
